﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Thksoft.UserCotrols.PrintCotrol
{
    public partial class PrinterVertical : System.Web.UI.UserControl
    {
        private bool _isPrinted = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            GetFontSize();
            BindPrintPortrait();
            if (!IsPostBack)
            {
                IsPrinted = Convert.ToBoolean(hdfIsPrinted.Value);
            }

            HtmlGenericControl ctl = new HtmlGenericControl("object");
            ctl.ID = "factory";
            ctl.Attributes["style"] = "display:none";
            ctl.Attributes["classid"] = "clsid:1663ed61-23eb-11d2-b92f-008048fdd814";
            ctl.Attributes["codebase"] = "/UserCotrols/PrintCotrol/smsx.cab#Version=6,4,438,06";

            HtmlGenericControl param = new HtmlGenericControl("param");
            param.Attributes.Add("name", "template");
            param.Attributes.Add("value", "MeadCo://IE7");

            ctl.Controls.Add(param);
            ((Page)Context.Handler).Page.Header.Controls.Add(ctl);

            btnPrintit.Attributes.Add("onclick", "printit()");
            btnPrintSetup.Attributes.Add("onclick", "printsetup()");
            btnPrintpreview.Attributes.Add("onclick", "printpreview()");
            ((Page)Context.Handler).ClientScript.RegisterClientScriptBlock(GetType(), ClientID, GetScriptBlock());
        }

        /// <summary>
        /// 获取或设置是否已打印本页。
        /// </summary>
        [Description("无说明"), Category("获取或设置是否已打印本页"), DefaultValue("false")]
        public bool IsPrinted
        {
            get
            {
                return _isPrinted;
            }
            set
            {
                _isPrinted = value;
            }
        }

        protected string GetScriptBlock()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendFormat(@"<script language='javascript' type='text/javascript'>
            function printit()
　　        {{
                 SetPrintPortrait();
                 //setPrintAttributes('','',true);
　　             document.getElementById('{0}td1').style.display = 'none';
                 document.getElementById('{0}td2').style.display = 'none';
　　             if(factory.printing.Print(false))
                 {{
                     document.getElementById('{0}hdfIsPrinted').value = 'true';
                 }}
                 else
                 {{
                     document.getElementById('{0}hdfIsPrinted').value = 'false';
                 }}
　　             document.getElementById('{0}td1').style.display = 'block';
                 document.getElementById('{0}td2').style.display = 'block';
　　        }}

            function printsetup()
            {{
　　            factory.printing.PageSetup();
　　        }}

　　        function printpreview()
            {{
                SetPrintPortrait();
                //setPrintAttributes('','',true);
　　            document.getElementById('{0}td1').style.display = 'none';
                document.getElementById('{0}td2').style.display = 'none';
　　            if(factory.printing.Preview())
                {{
                    document.getElementById('{0}hdfIsPreviewed').value = 'true';
                }}
                else
                {{
                     document.getElementById('{0}hdfIsPreviewed').value = 'false';
                }}
　　            document.getElementById('{0}td1').style.display = 'block';
                document.getElementById('{0}td2').style.display = 'block';
　　        }}

            function setPrintAttributes(headerText,footerText,printPortrait)
            {{
                var header = (typeof headerText=='string')?'':headerText;
                header=(header==null)?'':header;
                var footer = (typeof footerText=='string')?'':footerText;
                footer=(footer==null)?'':footer;
                top.console.log('-header:'+header+'-footer:'+footer);
                factory.printing.header = '&b'+header+'&b';
                factory.printing.footer = '&b'+footer+'&b';
                factory.printing.portrait = printPortrait;
                factory.printing.leftMargin =5.00;
                factory.printing.topMargin =5.00;
                factory.printing.rightMargin =15.00;
                factory.printing.bottomMargin =10.00;
           }}

           function ChangeSize()
           {{
               var sise = document.getElementById('{0}ddlFontSise').value;
               document.body.style.fontSize = sise;
           }}

           function SetPrintPortrait()
           {{
               var portrait = document.getElementById('{0}ddlPrintPortrait').value;
               setPrintAttributes('','',portrait);
           }}

        </script>", ClientID + "_");

            return sb.ToString();
        }

        protected void GetFontSize()
        {
            const int startsise = 6;
            const int length = 18;
            const string defaultValue = "9pt";
            for (int i = startsise; i < startsise + length; i++)
            {
                ddlFontSise.Items.Add(new ListItem(i.ToString() + "pt", i.ToString() + "pt"));
            }
            ddlFontSise.Value = defaultValue;
            ddlFontSise.Attributes.Add("onchange", "ChangeSize()");
        }

        protected void BindPrintPortrait()
        {
            ddlPrintPortrait.Items.Add(new ListItem("竖打", "true"));
            ddlPrintPortrait.Items.Add(new ListItem("横打", "false"));
        }
    }
}